#!/usr/bin/python
# -*- coding: utf-8 -*-

import re
import time
import Queue
import urllib
import socket
import MySQLdb
from sets import Set

socket.setdefaulttimeout(10)
q = Queue.PriorityQueue()
s = Set()

conn = MySQLdb.Connection("172.16.7.58", "root", "researchsucks", "X", 3306)
conn.set_character_set("utf8");
cur = conn.cursor()
out = open('/home/chaojiansong/videobook/pop/ver1/track_data/super.stat', 'w')

def process(data):
    ret = [time.mktime(data[7].timetuple())]
    for item in data:
    	ret.append(item)
    return ret

def get_info(reg, data):
    m = re.search(reg, data)
    if m:
    	return m.group(1)
    return -1

def update_info(info):
    url = "http://api.youku.com/api_ptvideoinfo/pid/XMTI5Mg==/id/" + info[1] + "/rt/xml"
    data = urllib.urlopen(url).read()
    
    info[2] = get_info(r'<pv>(.*)</pv>', data)
    info[3] = get_info(r'<totalcomment>(.*)</totalcomment>', data)
    info[4] = get_info(r'<fav>(.*)</fav>', data)
    info[5] = get_info(r'<up>(.*)</up>', data)
    info[6] = get_info(r'<down>(.*)</down>', data)
    info[7] = get_info(r'<reference>(.*)</reference>', data)
    
def record(data, flag):
    t = time.time() - data[0]
    if t >= 3600:
    	data[0] = time.time()
        if flag:
    	    update_info(data)
        out.write('id: ' + str(data[1]) + '\n')
        out.write('pv: ' + str(data[2]) + '\n')
        out.write('totalcomment: ' + str(data[3]) + '\n')
        out.write('fav: ' + str(data[4]) + '\n')
        out.write('up: ' + str(data[5]) + '\n')
        out.write('down: ' + str(data[6]) + '\n')
        out.write('reference: ' + str(data[7]) + '\n')
        out.write('pubdate: ' + str(data[8]) + '\n')
        out.write('updatetime: ' +time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(data[0])) + '\n')

while True:
    try:
        cur_time = time.strftime('%Y-%m-%d', time.localtime(time.time()))
        qq = Queue.PriorityQueue()
        if cur_time == '2011-12-18':
            cur.execute("select id, pv, totalcomment, fav, up, down, reference, pubdate from video_info where \
                    pubdate>=date('2011-12-18') and HOUR(pubdate)>=HOUR(NOW())-1")
            for data in cur.fetchall():
                if data[0] not in s:
                    s.add(data[0])
                    q.put(process(data))
        while not q.empty():
            try:
                head = q.get()
                record(head, True)
                qq.put(head)
            except Exception, why:
                print Exception, ':', why, 'flag'
                qq.put(head)
                record(head, False)
        q = qq
    except Exception, why:
        print Exception, ':', why

out.close()
cur.close()
conn.close()
